package com.kt.kvw.user.controller;

import com.kt.kvw.generator.AuthCodeGenerator;
import com.kt.kvw.model.respone.ResponseData;
import com.kt.kvw.user.config.AuthCodeConfig;
import com.kt.kvw.user.context.KvwContext;
import com.kt.kvw.user.context.KvwContextHolder;
import com.kt.kvw.user.model.form.RegisterForm;
import com.kt.kvw.user.service.UserService;
import com.kt.kvw.util.ResponseDataUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Desc:
 *
 * @author: keen
 * Date: 2020-07-04
 * Time: 23:08
 */
@RestController
@RequestMapping("/user")
@Validated
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private AuthCodeConfig authCodeConfig;

    @PostMapping("/register")
    public ResponseData register(@RequestBody @Valid RegisterForm registerForm, HttpServletRequest request) {
        String authCodeKey = getAuthCodeKey(request.getSession().getId());
        String redisAuthCode = stringRedisTemplate.opsForValue().get(authCodeKey);
        if (StringUtils.isBlank(redisAuthCode)) {
            return ResponseDataUtils.fail("验证码已过期");
        }
        if (!StringUtils.equals(registerForm.getAuthCode(), redisAuthCode)) {
            return ResponseDataUtils.fail("验证码错误");
        }
        if (userService.register(registerForm) > 0) {
            stringRedisTemplate.delete(authCodeKey);
            return ResponseDataUtils.success("注册成功", registerForm.getUserName());
        } else {
            return ResponseDataUtils.fail("注册失败");
        }
    }

    @PostMapping("/login")
    public ResponseData login(@RequestParam("userName") String userName,
                              @RequestParam("password") String password) {
        if (StringUtils.isBlank(userName)) {
            return ResponseDataUtils.fail("请输入用户名!");
        }
        if (StringUtils.isBlank(password)) {
            return ResponseDataUtils.fail("请输入密码!");
        }
        userService.login(userName, password);
        return null;
    }


    @GetMapping("/authCode")
    public ResponseData<String> authCode(HttpServletRequest request) {
        String authCode = AuthCodeGenerator.generatorWordAndNumberCode(6);
        Map<String, String> map = com.kt.kvw.util.MapUtils.of("authCode", authCode);
        stringRedisTemplate.opsForValue().set(getAuthCodeKey(request.getSession().getId()),
                authCode, authCodeConfig.getExpire(), TimeUnit.SECONDS);
        return ResponseDataUtils.success(map);
    }

    @GetMapping("/test")
    public String test(){
        return "test";
    }

    private String getAuthCodeKey(String sessionId) {
        return authCodeConfig.getPrefix() + sessionId;
    }
}
